package main

func uniquePathsWithObstacles(obstacleGrid [][]int) int {
	dp := make([]int, len(obstacleGrid[0]))

	if obstacleGrid[0][0] == 1 {
		return 0
	}

	dp[0] = 1
	for i := 1; i < len(dp); i++ {
		if obstacleGrid[0][i] == 0 {
			dp[i] = dp[i-1]
		}
	}

	getNum := func(i, j int) int {
		if j-1 >= 0 {
			return dp[j-1] + dp[j]
		}

		return dp[j]
	}

	for i := 1; i < len(obstacleGrid); i++ {
		for j := 0; j < len(obstacleGrid[0]); j++ {
			if obstacleGrid[i][j] == 0 {
				dp[j] = getNum(i, j)
			} else {
				dp[j] = 0
			}
		}
	}

	return dp[len(dp)-1]
}

func main() {
	uniquePathsWithObstacles([][]int{{0, 1, 0, 0, 0}, {1, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}})
}
